<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Im_cases extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');
	}

	public function index(){
	  $this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  $this->load->view($this->session->userdata('formain'),array(
	  		'case_type'			=>	$this->wus->get_lookup_values_by_type('CASE_TYPE'),
	  		'category'			=>	$this->wus->get_lookup_values_by_type('CATEGORY'),
	  		'sub_category'		=>	$this->wus->get_lookup_values_by_type('SUB_CATEGORY'),
	  		'department'		=>	$this->wus->get_lookup_values_by_type('DEPARTMENT'),
	  		'activity_status'	=>	$this->wus->get_lookup_values_by_type('ACTIVITY_STATUS'),
	  		'case_status'		=>	$this->wus->get_lookup_values_by_type('CASE_STATUS'),
	  ));
	  $this->load->view('layouts/footer');
	}
	
	public function add_form(){
		$this->load->view($this->session->userdata('foradd'),array(
				'case_type'			=>	$this->wus->get_lookup_values_by_type('CASE_TYPE'),
				'category'			=>	$this->im_cases_model->get_categories(),
				'sub_category'		=>	$this->wus->get_lookup_values_by_type('SUB_CATEGORY'),
				'impact'			=>	$this->wus->get_lookup_values_by_type('IMPACT'),
				'urgency'			=>	$this->wus->get_lookup_values_by_type('URGENCY'),
				'department'		=>	$this->wus->get_lookup_values_by_type('DEPARTMENT'),
				'case_status'		=>	$this->wus->get_lookup_values_by_type('CASE_STATUS'),
				'activity_status'	=>	$this->wus->get_lookup_values_by_type('ACTIVITY_STATUS'),
				'assigned_to'		=>	$this->im_cases_model->get_users(),
				'case_no'			=>  $this->im_cases_model->get_currval()
			));
	}

	public function edit_form($id){
		$this->load->view($this->session->userdata('foredit'),array(
				'im_cases'			=>	$this->im_cases_model->get_im_case_by_id($id),
				'case_type'			=>	$this->wus->get_lookup_values_by_type('CASE_TYPE'),
				'category'			=>	$this->im_cases_model->get_categories(),
				'sub_category'		=>	$this->wus->get_lookup_values_by_type('SUB_CATEGORY'),
				'impact'			=>	$this->wus->get_lookup_values_by_type('IMPACT'),
				'urgency'			=>	$this->wus->get_lookup_values_by_type('URGENCY'),
				'department'		=>	$this->wus->get_lookup_values_by_type('DEPARTMENT'),
				'case_status'		=>	$this->wus->get_lookup_values_by_type('CASE_STATUS'),
				'activity_status'	=>	$this->wus->get_lookup_values_by_type('ACTIVITY_STATUS'),
				'related_doc_type'	=>	$this->wus->get_lookup_values_by_type('RELATED_DOC_TYPE'),
				'action'			=>	$this->wus->get_lookup_values_by_type('ACTION'),
				'assigned_to'		=>	$this->im_cases_model->get_users(),
				'case_no'			=>  $this->im_cases_model->get_currval(),
				'work_order_case'	=>	$this->im_cases_model->getWorkOrderCaseId($id),
				'invoice_case'		=>	$this->im_cases_model->getInvoiceCaseId($id),
				'user_roles'		=>	$this->im_cases_model->get_role_by_user_id()
			));
	}

	public function view_form($id){
		$this->load->view($this->session->userdata('forview'),array(
				'im_cases'			=>	$this->im_cases_model->get_im_case_by_id($id),
				'case_type'			=>	$this->wus->get_lookup_values_by_type('CASE_TYPE'),
				'category'			=>	$this->im_cases_model->get_categories(),
				'sub_category'		=>	$this->wus->get_lookup_values_by_type('SUB_CATEGORY'),
				'impact'			=>	$this->wus->get_lookup_values_by_type('IMPACT'),
				'urgency'			=>	$this->wus->get_lookup_values_by_type('URGENCY'),
				'department'		=>	$this->wus->get_lookup_values_by_type('DEPARTMENT'),
				'case_status'		=>	$this->wus->get_lookup_values_by_type('CASE_STATUS'),
				'activity_status'	=>	$this->wus->get_lookup_values_by_type('ACTIVITY_STATUS'),
				'related_doc_type'	=>	$this->wus->get_lookup_values_by_type('RELATED_DOC_TYPE'),
				'assigned_to'		=>	$this->im_cases_model->get_users(),
				'case_no'			=>  $this->im_cases_model->get_currval(),
				'work_order_case'	=>	$this->im_cases_model->getWorkOrderCaseId($id),
				'invoice_case'		=>	$this->im_cases_model->getInvoiceCaseId($id)
			));
	}

	public function search_num(){
		echo json_encode($this->im_cases_model->search_num($this->input->post('caller_id')));		
	}

	public function get_related_doc_type_values(){
		echo json_encode($this->im_cases_model->get_related_doc_type_values($this->input->post('type')));
	}

	public function get_history_by_case_id(){
		echo json_encode($this->im_cases_model->get_history_by_case_id($this->input->post('id')));
	}

	public function get_sub_category_by_id(){
		echo json_encode($this->im_cases_model->get_sub_category_by_id($this->input->post('id')));
	}

	public function get_sub_category_by_category_id(){
		echo json_encode($this->im_cases_model->get_sub_category_by_category_id($this->input->post('id')));
	}
	
	public function get_category_by_id(){
		echo json_encode($this->im_cases_model->get_category_by_id($this->input->post('id')));
	}

	public function get_user_by_department(){
		echo json_encode($this->im_cases_model->get_user_by_department($this->input->post('id')));
	}

	public function search(){
		$data = array(
			'case_no'			=>	$this->input->post('case_no'),
			'case_date'		=>	$this->input->post('case_date'),
			'case_type'		=>	$this->input->post('case_type'),
			'caller_acct_id'	=>	$this->input->post('caller_acct_id'),
			'category'		=>	$this->input->post('category'),
			'sub_category'	=>	$this->input->post('sub_category'),
			'department'		=>	$this->input->post('department'),
			'activity_status'	=>	$this->input->post('activity_status'),
			'case_status'		=>	$this->input->post('case_status'),
			'my_cases'			=>	($this->input->post('my_case') == "on" ? $this->session->userdata('user_id') : "")
		);
		echo json_encode($this->im_cases_model->search($data));
	}

	public function insert(){
		$data = array(
			'p_case_no'			=>	$this->input->post('case_no'),
			'p_case_date'		=>	$this->input->post('case_date'),
			'p_case_type'		=>	$this->input->post('case_type'),
			'p_caller_acct_id'	=>	$this->input->post('caller_acct_id'),
			'p_caller_name'		=>	$this->input->post('caller_name'),
			'p_caller_address'	=>	$this->input->post('caller_address'),
			'p_caller_contact'	=>	$this->input->post('caller_contact'),
			'p_category'		=>	$this->input->post('category'),
			'p_sub_category'	=>	$this->input->post('sub_category'),
			'p_impact'			=>	$this->input->post('impact'),
			'p_urgency'			=>	$this->input->post('urgency'),
			'p_case_title'		=>	$this->input->post('case_title'),
			'p_case_details'	=>	$this->input->post('case_details'),
			'p_desired_date'	=>	$this->input->post('desired_date'),
			'p_department'		=>	$this->input->post('department'),
			'p_assigned_to'		=>	$this->input->post('assigned_to'),
			'p_activity_status'	=>	$this->input->post('activity_status'),
			'p_case_status'		=>	$this->input->post('case_status'),
			'p_root_cause'		=>	$this->input->post('root_cause'),
			'p_date_resolved'	=>	$this->input->post('date_resolved'),
			'p_user_id'			=>	$this->session->userdata('user_id'),
			'p_reported_date'	=>	$this->input->post('reported_date')
		);
		$this->im_cases_model->insert_case($data);
	}

	public function update(){
		$data = array(
			'p_case_no'			=>	$this->input->post('case_no'),
			'p_case_date'		=>	$this->input->post('case_date'),
			'p_case_type'		=>	$this->input->post('case_type'),
			'p_caller_acct_id'	=>	$this->input->post('caller_id'),
			'p_caller_name'		=>	$this->input->post('caller_name'),
			'p_caller_address'	=>	trim($this->input->post('caller_address')),
			'p_caller_contact'	=>	$this->input->post('caller_contact'),
			'p_category'		=>	$this->input->post('category'),
			'p_sub_category'	=>	$this->input->post('sub_category'),
			'p_impact'			=>	$this->input->post('impact'),
			'p_urgency'			=>	$this->input->post('urgency'),
			'p_case_title'		=>	trim($this->input->post('case_title')),
			'p_case_details'	=>	trim($this->input->post('case_details')),
			'p_desired_date'	=>	$this->input->post('desired_date'),
			'p_department'		=>	$this->input->post('department'),
			'p_assigned_to'		=>	$this->input->post('assigned_to'),
			'p_activity_status'	=>	$this->input->post('activity_status'),
			'p_case_status'		=>	$this->input->post('case_status'),
			'p_root_cause'		=>	trim($this->input->post('root_cause')),
			'p_date_resolved'	=>	$this->input->post('date_resolved'),
			'p_user_id'			=>	$this->session->userdata('user_id'),
			'p_reported_date'	=>	$this->input->post('reported_date'),
			'p_case_id'			=>	$this->input->post('case_id'),
		);
		$this->im_cases_model->update_case($data);
	}

	public function insert_history(){
		$data = array(
			'p_case_id'				=>	$this->input->post('case_id'),
			'p_action'				=>	$this->input->post('action'),
			'p_comments'			=>	$this->input->post('comments'),
			'p_root_cause'			=>	$this->input->post('root_cause'),
			'p_related_doc_type'	=>	$this->input->post('related_doc_type'),
			'p_related_doc_no'		=> 	$this->input->post('related_doc_no'),
			'p_action_date'			=>	(empty($this->input->post('action_date')) ? date('Y-m-d H:i:S') : $this->input->post('action_date')),
			'p_user_id'				=> 	$this->session->userdata('user_id'),
			'p_assigned_to'			=>	$this->input->post('assigned_to')
		);
		$this->im_cases_model->insert_history($data);
	}
}